package com.splic.api.controller.product;

import com.github.pagehelper.PageInfo;
import com.splic.common.core.controller.BaseController;
import com.splic.common.core.domain.ResponseObj;
import com.splic.common.exception.BusinessException;
import com.splic.common.utils.ShiroUtils;
import com.splic.product.domain.UserFriendRelation;
import com.splic.product.service.UserFriendRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户好友关系
 */
@RestController
@RequestMapping("/api/userFriend")
@Slf4j
public class ApiUseFriendsController extends BaseController {

    @Autowired
    private UserFriendRelationService userFriendRelationServiceImpl;
    /**
     * 保存用户好友关系
     *
     * @return
     */
    @PostMapping("/saveFriendsRelation")
    public ResponseObj saveFriendsRelation(@RequestBody UserFriendRelation userFriendRelation) {
        try {
            if(userFriendRelation.getBeUserId() == null){
                return ResponseObj.ERROR("被绑定人参数为空");
            }
            userFriendRelation.setUserId(ShiroUtils.getUserId());
            int count = userFriendRelationServiceImpl.insert(userFriendRelation);
            if(count <= 0){
                throw new BusinessException("用户浏览记录保保存用户好友关系失败");
            }
            return ResponseObj.SUCCESS();
        }catch (Exception e){
            log.error("保存用户好友关系失败:{}",e);
            return ResponseObj.ERROR("保存用户好友关系失败");
        }
    }

    /**
     * 获取用户好友关系列表
     *
     * @return
     */
    @GetMapping("/getFriendRelation")
    public ResponseObj<PageInfo<UserFriendRelation>> getFriendRelation() {
        try{
            startPage();
            UserFriendRelation userFriendRelation = new UserFriendRelation();
            userFriendRelation.setUserId(ShiroUtils.getUserId());
            List<UserFriendRelation> list = userFriendRelationServiceImpl.selectPageList(userFriendRelation);
            return ResponseObj.SUCCESS(new PageInfo(list));
        }catch (Exception e){
            log.error("获取用户好友关系列表失败:{}",e);
            return ResponseObj.ERROR("获取用户好友关系列表失败");
        }
    }
}
